package com.youku.adSDK.view
{
   import flash.display.MovieClip;
   import com.youku.adSDK.view.components.OverlayAD;
   import com.youku.adSDK.ADEventDispather;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.YoukuAdManager;
   import flash.utils.getTimer;
   import com.youku.adSDK.model.vo.ADData;
   import com.youku.adSDK.model.vo.PaikeData;
   import com.youku.adSDK.view.events.FlashADEvent;
   import com.youku.adSDK.YoukuAdEvent;
   
   public class OverlayMediator extends Object
   {
      
      public function OverlayMediator(param1:ADEventDispather, param2:ADContext) {
         this._points = [];
         super();
         this._mainMc = param2.mainMc;
         this._context = param2;
         this._eventDispather = param1;
      }
      
      private var _mainMc:MovieClip;
      
      private var _overlay:OverlayAD;
      
      private var _width:Number = 0;
      
      private var _height:Number = 0;
      
      private var _barH:Number = 0;
      
      private var _eventDispather:ADEventDispather;
      
      private var _context:ADContext;
      
      private var _isPause:Boolean = true;
      
      private var _lastCountedTime:Number = 0;
      
      private var _playedTime:Number = 0;
      
      private var _isReplay:Boolean = false;
      
      private var _index:int = 0;
      
      private var _points:Array;
      
      private var _isFirstRequest:Boolean = false;
      
      private var _isFirstDataLoaded:Boolean = false;
      
      public function init() : void {
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
         this._eventDispather.addEventListener(ADEvent.COMMON_RESET,this.onPlayerReset);
         this._eventDispather.addEventListener(ADEvent.REQUEST_AND_PLAY_BACK_AD,this.onPlayerShowBackAD);
         this._eventDispather.addEventListener(ADEvent.PLAYER_PAUSE,this.onPlayerPause);
         this._eventDispather.addEventListener(ADEvent.PLAYER_RESUME,this.onPlayerResume);
         this._eventDispather.addEventListener(ADEvent.VIDEO_PLAYING,this.onPlayerPlaying);
         this._eventDispather.addEventListener(ADEvent.VIDEO_END,this.onPlayerOver);
         this.addEvents(true);
      }
      
      private function addEvents(param1:Boolean) : void {
         if(param1)
         {
            this._eventDispather.addEventListener(ADEvent.OVERLAY_DATA_LOADED,this.onADEvent);
            this._eventDispather.addEventListener(ADEvent.OVERLAY_DATA_LOAD_ERROR,this.onADEvent);
            this._eventDispather.addEventListener(ADEvent.INSERT_READY_TO_PLAY,this.onADEvent);
            this._eventDispather.addEventListener(ADEvent.INSERT_PLAY_DONE,this.onADEvent);
         }
         else
         {
            this._eventDispather.removeEventListener(ADEvent.OVERLAY_DATA_LOADED,this.onADEvent);
            this._eventDispather.removeEventListener(ADEvent.OVERLAY_DATA_LOAD_ERROR,this.onADEvent);
            this._eventDispather.removeEventListener(ADEvent.INSERT_READY_TO_PLAY,this.onADEvent);
            this._eventDispather.removeEventListener(ADEvent.INSERT_PLAY_DONE,this.onADEvent);
         }
      }
      
      private function onPlayerPlaying(param1:ADEvent) : void {
         if(!param1.data.nsTime || (YoukuAdManager.frontPlaying) || (this._isReplay))
         {
            return;
         }
         if(!this._isFirstRequest)
         {
            this.loadData();
            this._isFirstRequest = true;
         }
         var _loc2_:Number = getTimer();
         if(this._isPause)
         {
            this._lastCountedTime = _loc2_;
         }
         var _loc3_:Number = 0;
         if(this._lastCountedTime != 0)
         {
            _loc3_ = _loc2_ - this._lastCountedTime;
         }
         this._playedTime = this._playedTime + _loc3_;
         this._lastCountedTime = _loc2_;
         if((this._isPause) || !this._isFirstDataLoaded)
         {
            return;
         }
         var _loc4_:int = Math.floor(this._playedTime / 1000);
         if((this._overlay) && (!(this._overlay.endTime == -1)) && _loc4_ >= this._overlay.endTime)
         {
            this.removeAD();
         }
         if((this._points) && (this._index < this._points.length) && _loc4_ >= this._points[this._index])
         {
            this.loadData();
         }
         if((this._points) && this._index >= this._points.length - 1)
         {
            this.removeVideoPlaying();
         }
      }
      
      private function onPlayerOver(param1:ADEvent) : void {
         this.removeVideoPlaying();
      }
      
      private function onADEvent(param1:ADEvent) : void {
         var _loc2_:ADData = null;
         var _loc3_:* = 0;
         switch(param1.type)
         {
            case ADEvent.OVERLAY_DATA_LOADED:
               _loc2_ = param1.data.addata as ADData;
               _loc3_ = param1.data.ps;
               if(_loc2_.AL == -1)
               {
                  this.removeVideoPlaying();
               }
               if(this._index == 1)
               {
                  this._points = param1.data.ats as Array;
               }
               if(_loc3_ == this._index - 1)
               {
                  this.initOverlayAD(_loc2_);
               }
               this._isFirstDataLoaded = true;
               break;
            case ADEvent.OVERLAY_DATA_LOAD_ERROR:
               if((this._index == 1) && (param1.data) && (param1.data.hasOwnProperty("ats")))
               {
                  this._points = param1.data.ats as Array;
                  this._isFirstDataLoaded = true;
               }
               if(this._context.isPaike == true && this._context.winType == "exterior" && this._index == 1)
               {
                  this.initOverlayAD(PaikeData.data);
               }
               break;
            case ADEvent.INSERT_READY_TO_PLAY:
               if(this._overlay)
               {
                  this._overlay.show(false);
               }
               break;
            case ADEvent.INSERT_PLAY_DONE:
               if(this._overlay)
               {
                  this._overlay.show(true);
               }
               break;
         }
      }
      
      private function removeVideoPlaying() : void {
         this._eventDispather.removeEventListener(ADEvent.VIDEO_PLAYING,this.onPlayerPlaying);
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._width = param1.data.width;
         this._height = param1.data.height;
         this._barH = 40;
      }
      
      private function onPlayerReset(param1:ADEvent) : void {
         this.addEvents(false);
         if(this._overlay)
         {
            this._overlay.dispose();
         }
         this._isReplay = true;
      }
      
      private function onPlayerShowBackAD(param1:ADEvent) : void {
         this._isPause = true;
         if(this._overlay)
         {
            this._overlay.dispose();
         }
      }
      
      private function onPlayerPause(param1:ADEvent) : void {
         this._isPause = true;
         if((param1.data.useraction) && (this._overlay))
         {
            this._overlay.show(false);
         }
      }
      
      private function onPlayerResume(param1:ADEvent) : void {
         this._isPause = false;
         if(this._overlay)
         {
            this._overlay.show(true);
         }
      }
      
      private function loadData() : void {
         if(this._context.isTudouPlayer)
         {
            return;
         }
         this._eventDispather.dispatchEvent(new ADEvent(ADEvent.DO_OVERLAY,this._index));
         this._index++;
      }
      
      private function initOverlayAD(param1:ADData) : void {
         var _loc3_:* = NaN;
         this.removeAD();
         this._overlay = new OverlayAD();
         this._mainMc.addChild(this._overlay);
         this._overlay.addEventListener(FlashADEvent.CONTENT_LOAD_ERROR,this.onContentLoadError);
         this._overlay.addEventListener(FlashADEvent.CONTENT_GET_FOCUS,this.onContentGetFocus);
         this._overlay.addEventListener(FlashADEvent.CONTENT_LOST_FOCUS,this.onContentLostFocus);
         this._overlay.addEventListener(FlashADEvent.CONTENT_CLOSE,this.onAdClose);
         if(!this._width)
         {
            this._width = this._mainMc.stage.stageWidth;
            this._height = this._mainMc.stage.stageHeight;
            this._barH = 40;
         }
         var _loc2_:Number = 0;
         _loc2_ = Number(param1.AL);
         if(param1.AL != -1)
         {
            _loc3_ = this._points[this._index - 1];
            _loc2_ = _loc2_ + _loc3_;
         }
         this._overlay.init(param1,_loc2_,this._width,this._height,this._barH,this._context,this._eventDispather);
      }
      
      private function onAdClose(param1:FlashADEvent) : void {
         this.removeAD();
      }
      
      private function onContentLostFocus(param1:FlashADEvent) : void {
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_LOST_FOCUS));
      }
      
      private function onContentGetFocus(param1:FlashADEvent) : void {
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_GET_FOCUS));
      }
      
      private function onContentLoadError(param1:FlashADEvent) : void {
         this._overlay.dispose();
      }
      
      public function removeAD() : void {
         if(this._overlay)
         {
            this._overlay.dispose();
            if(this._overlay.parent)
            {
               this._mainMc.removeChild(this._overlay);
            }
            this._overlay = null;
         }
      }
   }
}
